import { Component, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { UserEditComponent } from '../user-edit/user-edit.component';
import { ApiService } from '@core/services/api';
import { STData } from '@delon/abc';
import { ACLService } from '@delon/acl';

@Component({
  selector: 'app-user-list',
  templateUrl: './user-list.component.html',
  styles: []
})
export class UserListComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  selectData: any[];
  loading = false;
  addFlag:boolean = this.aclService.canAbility("user-manage/add");//检查用户按钮权限
  editFlag:boolean =  this.aclService.canAbility("user-manage/edit");
  delFlag:boolean =  this.aclService.canAbility("user-manage/delete");
  resetFlag:boolean =  this.aclService.canAbility("user-manage/reset");
  addUserInfo = {
    userAccount: '',
    userPassword: '',
    userName: '',
    roles: [],
    depts: []
  };
  config: SPage = {
    columns: [
      {
        index: 'id',
        title: '多选',
        type: ColumnType.checkbox,
      },
      {
        index: 'userName',
        title: '用户名称',
        filter: {
          placeholder: '请输入用户名称',
          type: 'string'
        }
      },
      {
        index: 'userAccount',
        title: '用户账号',
      },
      {
        index: 'roleName',
        title: '角色名称',
        filter: {
          placeholder: '请输入角色名称',
          type: 'string'
        }
      },
      // {
      //   index: 'deptName',
      //   title: '权限区域',
      // },
      {
        index: 'status',
        title: '数据状态',
        type: ColumnType.tag,
        tag: {
          false: {text:'停用', color: 'red'},
          true: {text:'启用', color: 'green'},
        },
        filter: {
          placeholder: '请选择数据状态',
          type: 'select',
          items: [
            {label: '启用', value: true},
            {label: '停用', value: false},
          ]
        }
      },
      {
        index: 'userRemark',
         title: '备注'
      },
    ],
    showCheckBox: true,
    reqUrl:this.api.userList.url,
    editBtn: [
      { title: '编辑',iif: () => this.editFlag }
    ]
  }
  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService, private message: NzMessageService,private aclService: ACLService,) { }

  ngOnInit() {

  }
  // 表格预处理
  dataProcess(data: STData){
    // console.log(data)
     const res =  data.map((i: STData, index: number) => {
      if(i.roles && i.roles.length>0){
        i.roleType = ''
        i.roles.forEach(item => {
          if(item.roleName){
            i.roleType += item.roleName+'；';
          }
        });
        if(i.roleType.length > 0)
        i.roleType = i.roleType.substring(0, i.roleType.length - 1);
      }
      if(i.depts && i.depts.length>0){
        i.deptType = ''
        i.depts.forEach(item => {
          if(item.deptName){
            i.deptType += item.deptName+'；';
          }
        });
        if(i.deptType.length > 0)
        i.deptType = i.deptType.substring(0, i.deptType.length - 1);
      }
      return i;
    });
    console.log(res)
    return res;

  }

  // 编辑功能开始
  editDic(type: 0 | 1 = 0, _param?: object): void {
    const modal = this.modal.create({
      nzTitle: '用户信息编辑',
      nzContent: UserEditComponent,
      nzWidth: 800,
      nzComponentParams: {
        type,
        _param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
      }
    })
  }
  operate(e: any): void {
    if (e.title === '编辑') {
      this.editDic(1, e.data);
    }
  }
  _console(e: any): void {
    this.selectData = e;
  }
  // 删除功能
  delData() {
    if (!!this.selectData && this.selectData.length !== 0) {
      this.modal.confirm({
        nzTitle: '您确定要删除这些数据吗',
        nzOnOk: () => {
          const body = [];
          this.selectData.forEach(item => {
            body.push({id: item.id})
          })
          this.http.put(this.api.user.delete.url,body).subscribe(res => {
            if (res.status === '200') {
              this.message.success('删除成功');
              this.st.reload();
            }
          })
        }
      })
    } else {
      this.message.warning('请选择数据');
    }
  }
  // 重置密码
  resetPassword() {
    if (!!this.selectData && this.selectData.length > 0) {
      this.modal.confirm({
        nzTitle: '您确定要重置密码吗',
        nzOnOk: () => {
          const body = [];
          this.selectData.forEach(item => {
            body.push({id: item.id});
          })
          this.http.put(this.api.user.resetPassword.url,body).subscribe(res => {
            if (res.status === '200') {
              this.message.success('密码重置成功');
              this.st.reload();
            }
          })
        }
      })
    } else {
      this.message.warning('请选择数据后操作');
    }
  }

}
